/*************************************************
	Validator v1.05
	code by 我佛山人	
	wfsr@msn.com

	modify by 我是乐东人
	yuyu_xn@yahoo.com.cn
	2007-9-9
*************************************************/
/**************************************************************
 【修改说明】原框架存在以下缺点：
 1、文档化不高，注释太少，不方便扩展。
 2、校验的对象只能是类型等少量的规则。
 3、当被校验的对象有多个规则时，提示只能有一个，不够精细。
 4、不能对一个对象提供多个校验规则，并且它们之间有依赖关系。
 *************************************************************/
	
	Validator.English = /^[A-Za-z]+$/;
	Validator.msgEnglish = "英文名只允许英文字母";

	Validator.Email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	Validator.Phone = /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/;
	Validator.Mobile = /^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$/;
	Validator.Url = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
	Validator.IdCard ="this.IsIdCard(value)";
	Validator.Currency = /^\d+(\.\d+)?$/;
	Validator.Number = /^\d+$/;
	Validator.Zip = /^[1-9]\d{5}$/;
	Validator.QQ = /^[1-9]\d{4,8}$/;
	Validator.Integer = /^[-\+]?\d+$/;
	Validator.Double = /^[-\+]?\d+(\.\d+)?$/;
	Validator.Chinese =  /^[\u0391-\uFFE5]+$/;
	Validator.Username = /^[a-z]\w{3,}$/i;
	Validator.UnSafe =/^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/;
	
	Validator.IsSafe = function(str){return !this.UnSafe.test(str);};
	Validator.SafeString = "this.IsSafe(value)";
	Validator.Filter = "this.DoFilter(value, getAttribute('accept'))";
	Validator.Limit = "this.limit(value.length,getAttribute('min'),  getAttribute('max'))";
	Validator.LimitB = "this.limit(this.LenB(value), getAttribute('min'), getAttribute('max'))";
	Validator.Date = "this.IsDate(value, getAttribute('min'), getAttribute('format'))";
	Validator.Repeat = "value == document.getElementsByName(getAttribute('to'))[0].value";
	/**
	 * 是否输入的值属于指定的范围中
	 * @ min 最小值
	 * @ max 最大值
	 */
	Validator.Range = "getAttribute('min') < (value|0) && (value|0) < getAttribute('max')";

	/**
	 * 比较两个输入值是否符合给定的操作
	 * @ value 当前值
	 * @ operator 操作【NotEqual、GreaterThan、GreaterThanEqual、LessThan、LessThanEqual】
	 * @ to 被比较的属性
	 */
	Validator.Compare = "this.compare(value,getAttribute('operator'),getAttribute('to'))";
	Validator.Custom = "this.Exec(value, getAttribute('regexp'))";
	Validator.Group = "this.MustChecked(getAttribute('name'), getAttribute('min'), getAttribute('max'))";

	/*********************************************************************************************
	 * 功能：提供一个比较两个日期是否在用户指定的范围内
	 * @diff 此属性要求程序员指定日期范围，比如7天， diff="7"
	 * @to 此属性指定要比较的域
	 ********************************************************************************************/
	Validator.CompareDate = "this.compareDate(value,getAttribute('diff'),document.getElementsByName(getAttribute('to'))[0].value)";
	
	Validator.compareDate = function(endDate,diff,startDate){
		var e = endDate.split("-");
		var s = startDate.split("-");
		
		var sDate = new Date(s[0],s[1],s[2]);
		var eDate = new Date(e[0],e[1],e[2]);

		var diffs = eDate.getTime() - sDate.getTime();

		var date = diffs/(1000 * 24 * 60 * 60);

		if(date > diff) {
			return false;
		}
		else {
			return true;
		}
	};
	//add by chenfuzhong 2007-9-9 end

	/**************************************************************************************************
	 *  扩展未有的业务规则,步骤1：
	 *  增加规则名称及其回调代码，如下： 
	 *  CompareDate : "this.compareDate(value,getAttribute('diff'),document.getElementsByName(getAttribute('to'))[0].value)",
	 *
	 *   注：可以自定义属性，如diff，当compareDate被调用的时候，传入的参数是diff的value，
	 **************************************************************************************************/

	
	
	Validator.limit = function(len,min, max){
		min = min || 0;
		max = max || Number.MAX_VALUE;
		return min <= len && len <= max;
	};
	Validator.LenB = function(str){
		return str.replace(/[^\x00-\xff]/g,"**").length;
	};
	Validator.ClearState = function(elem){
		with(elem){
			if(style.color == "red")
				style.color = "";
			var lastNode = parentNode.childNodes[parentNode.childNodes.length-1];
			if(lastNode.id == "__ErrorMessagePanel")
				parentNode.removeChild(lastNode);
		}
	};
	Validator.AddError = function(index, str){
		this.ErrorItem[this.ErrorItem.length] = this.ErrorItem[0].elements[index];
		this.ErrorMessage[this.ErrorMessage.length] = this.ErrorMessage.length + ":" + str;
	};
	Validator.Exec = function(op, reg){
		return new RegExp(reg,"g").test(op);
	};	
	Validator.compare = function(op1,operator,op2){
		switch (operator) {
			case "NotEqual":
				return (op1 != op2);
			case "GreaterThan":
				return (op1 > op2);
			case "GreaterThanEqual":
				return (op1 >= op2);
			case "LessThan":
				return (op1 < op2);
			case "LessThanEqual":
				return (op1 <= op2);
			default:
				return (op1 == op2);            
		}
	};
	
	// add by chenfuzhong 2007-9-9 begin
	/**************************************************
	 * 增加自定义函数校验类型
	 *
	 * @fname 自定义的函数名称
	 * @funcParams 自定义函数的参数名称数组
	 *************************************************/
	Validator.customFunction = function(fname,funcParams){
		var sParaNames = funcParams.split(",");
		var sParaValues = new Array();
		var exeJs = fname + "(";
		for(var i = 0 ; i < sParaNames.length; i++ ) {
			exeJs += "'" + document.getElementsByName(sParaNames[i])[0].value + "'";
			if(i < sParaNames.length - 1) {
				exeJs += ",";
			}
		}
		exeJs += ")";
		//据说使用eval函数进行回调 不安全
		if(!eval(exeJs))	{
			return false;
		}
		return true;
		
	};
	//add by chenfuzhong 2007-9-9 end
	/********************************************************************************
	 *  扩展未有的业务规则,步骤3：
	 *  增加未有业务的回调函数，如下： 
	 *  比较给定的两个日期之间的天数不能大于diff
	 *
	 *  compareDate : function(endDate,diff,startDate){
			var e = endDate.split("-");
			var s = startDate.split("-");
			
			var sDate = new Date(s[0],s[1],s[2]);
			var eDate = new Date(e[0],e[1],e[2]);

			var diffs = eDate.getTime() - sDate.getTime();

			var date = diffs/(1000 * 24 * 60 * 60);

			if(date > diff) {
				return false;
			}
			else {
				return true;
			}
	    },
	   ********************************************************************************/

	Validator.MustChecked = function(name, min, max){
		var groups = document.getElementsByName(name);
		var hasChecked = 0;
		min = min || 1;
		max = max || groups.length;
		for(var i=groups.length-1;i>=0;i--)
			if(groups[i].checked) hasChecked++;
		return min <= hasChecked && hasChecked <= max;
	};

	Validator.DoFilter = function(input, filter){
		return new RegExp("^.+\.(?=EXT)(EXT)$".replace(/EXT/g, filter.split(/\s*,\s*/).join("|")), "gi").test(input);
	};
	Validator.IsIdCard = function(number){
		var date, Ai;
		var verify = "10x98765432";
		var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
		var area = ['','','','','','','','','','','','北京','天津','河北','山西','内蒙古','','','','','','辽宁','吉林','黑龙江','','','','','','','','上海','江苏','浙江','安微','福建','江西','山东','','','','河南','湖北','湖南','广东','广西','海南','','','','重庆','四川','贵州','云南','西藏','','','','','','','陕西','甘肃','青海','宁夏','新疆','','','','','','台湾','','','','','','','','','','香港','澳门','','','','','','','','','国外'];
		var re = number.match(/^(\d{2})\d{4}(((\d{2})(\d{2})(\d{2})(\d{3}))|((\d{4})(\d{2})(\d{2})(\d{3}[x\d])))$/i);
		if(re == null) return false;
		if(re[1] >= area.length || area[re[1]] == "") return false;
		if(re[2].length == 12){
			Ai = number.substr(0, 17);
			date = [re[9], re[10], re[11]].join("-");
		}
		else{
			Ai = number.substr(0, 6) + "19" + number.substr(6);
			date = ["19" + re[4], re[5], re[6]].join("-");
		}
		if(!this.IsDate(date, "ymd")) return false;
		var sum = 0;
		for(var i = 0;i<=16;i++){
			sum += Ai.charAt(i) * Wi[i];
		}
		Ai +=  verify.charAt(sum%11);
		return (number.length ==15 || number.length == 18 && number == Ai);
	};
	Validator.IsDate = function(op, formatString){
		formatString = formatString || "ymd";
		var m, year, month, day;
		switch(formatString){
			case "ymd" :
				m = op.match(new RegExp("^((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})$"));
				if(m == null ) return false;
				day = m[6];
				month = m[5]*1;
				year =  (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10));
				break;
			case "dmy" :
				m = op.match(new RegExp("^(\\d{1,2})([-./])(\\d{1,2})\\2((\\d{4})|(\\d{2}))$"));
				if(m == null ) return false;
				day = m[1];
				month = m[3]*1;
				year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10));
				break;
			default :
				break;
		}
		if(!parseInt(month)) return false;
		month = month==0 ?12:month;
		var date = new Date(year, month-1, day);
        return (typeof(date) == "object" && year == date.getFullYear() && month == (date.getMonth()+1) && day == date.getDate());
		function GetFullYear(y){return ((y<30 ? "20" : "19") + y)|0;}
	};
 
